//---------------------------------------------------------------------------------
#include <CTestMap.h>
#include <CMapReader.h>
//---------------------------------------------------------------------------------
void CTestMap::initTestCase(void) {
}
//---------------------------------------------------------------------------------
void CTestMap::getCaseTest(void) {
	CMap map;
	CCase* c1=new CCase(0, 0, CCase::ectMur);
	map.addCase(c1);
	
	QCOMPARE(c1, map.getCase(0, 0));
	QCOMPARE(c1, map.getCase(0));
	QCOMPARE((CCase*)0, map.getCase(-1));
	QCOMPARE((CCase*)0, map.getCase(1));
	
	CCase* c2=new CCase(1, 1, CCase::ectMur);
	map.addCase(c2);
	
	CCase* c=map.getCase(0);
	if(c == c1) {
		QCOMPARE(c2, map.getCase(1));
	}else {
		QCOMPARE(c1, map.getCase(1));
	}
}
//---------------------------------------------------------------------------------
void CTestMap::getNbCaseTest(void) {
	CMap map;
	CCase *c1=new CCase(0, 0, CCase::ectMur);
	map.addCase(c1);
	
	QCOMPARE(1, map.getNbCase());
}
//---------------------------------------------------------------------------------
void CTestMap::addCaseTest(void) {
	CMap map;
	CCase *c1=new CCase(0, 0, CCase::ectMur);
	map.addCase(c1);
	
	CCase* c2=new CCase(0, 0, CCase::ectVide);
	QCOMPARE(map.addCase(c2), false);
	delete c2;
	
	c2=new CCase(1, 0, CCase::ectVide);
	QCOMPARE(map.addCase(c2), true);
	
	c2=new CCase(-1, 0, CCase::ectVide);
	QCOMPARE(map.addCase(c2), false);
	delete c2;
	
	c2=new CCase(0, -1, CCase::ectVide);
	QCOMPARE(map.addCase(c2), false);
	delete c2;
}
//---------------------------------------------------------------------------------
void CTestMap::canMoveUpTest(void) {
	CMap map;
	CCase *c1=new CCase(0, 0, CCase::ectMur);
	map.addCase(c1);
	
	CCase *c2=new CCase(0, 1, CCase::ectVide);
	map.addCase(c2);
	
	QCOMPARE(map.canMoveUp(0, 1), false);
	QCOMPARE(map.canMoveUp(0, 0), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canMoveDownTest(void) {
	CMap map;
	CCase *c1=new CCase(0, 0, CCase::ectMur);
	map.addCase(c1);
	
	CCase *c2=new CCase(0, 1, CCase::ectVide);
	map.addCase(c2);
	
	QCOMPARE(map.canMoveDown(0, 1), false);
	QCOMPARE(map.canMoveDown(0, 0), true);
	c2->setHasCaisse(true);
	QCOMPARE(map.canMoveDown(0, 0), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canMoveRightTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			"  *\n";
	level+=			"#.  #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.canMoveRight(2, 1), true);
	QCOMPARE(map.canMoveRight(3, 1), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canMoveLeftTest(void) {
	QString level=	" # #\n";
	level+=			"# $ #\n";
	level+=			"     \n";
	level+=			"#   #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.canMoveLeft(2, 1), true);
	QCOMPARE(map.canMoveLeft(1, 1), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canPushUpTest(void) {
	QString level=	" # #\n";
	level+=			"#  $#\n";
	level+=			"  * \n";
	level+=			"# $ #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.canPushUp(2, 3), true);
	QCOMPARE(map.canPushUp(3, 2), false);
	QCOMPARE(map.canPushUp(2, 4), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canPushDownTest(void) {
	QString level=	" # #\n";
	level+=			"# $ #\n";
	level+=			"  * \n";
	level+=			"#*  #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.canPushDown(2, 1), true);
	QCOMPARE(map.canPushDown(2, 0), false);
	QCOMPARE(map.canPushDown(1, 2), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canPushRightTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			" $* \n";
	level+=			"#  $#\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.canPushRight(1, 2), true);
	QCOMPARE(map.canPushRight(0, 2), false);
	QCOMPARE(map.canPushRight(2, 3), false);
}
//---------------------------------------------------------------------------------
void CTestMap::canPushLeftTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			"  *$ \n";
	level+=			"#$  #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.canPushLeft(3, 2), true);
	QCOMPARE(map.canPushLeft(4, 2), false);
	QCOMPARE(map.canPushLeft(2, 3), false);
}
//---------------------------------------------------------------------------------
void CTestMap::pushUpTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			"  $  \n";
	level+=			"# @ #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	map.pushUp();
	
	int xp, yp;
	map.getPlayerPos(xp, yp);
	
	QCOMPARE(map.getCase(2 ,1)->hasCaisse(), true);
	QCOMPARE(xp, 2);
	QCOMPARE(yp, 2);
}
//---------------------------------------------------------------------------------
void CTestMap::pushDownTest(void) {
	QString level=	" # #\n";
	level+=			"# @ #\n";
	level+=			"  $  \n";
	level+=			"#   #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	map.pushDown();
	
	int xp, yp;
	map.getPlayerPos(xp, yp);
	
	QCOMPARE(map.getCase(2 ,3)->hasCaisse(), true);
	QCOMPARE(xp, 2);
	QCOMPARE(yp, 2);
}
//---------------------------------------------------------------------------------
void CTestMap::pushRightTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			" @$  \n";
	level+=			"#   #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	map.pushRight();
	
	int xp, yp;
	map.getPlayerPos(xp, yp);
	
	QCOMPARE(map.getCase(3 ,2)->hasCaisse(), true);
	QCOMPARE(xp, 2);
	QCOMPARE(yp, 2);
}
//---------------------------------------------------------------------------------
void CTestMap::pushLeftTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			"  $@ \n";
	level+=			"#   #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	map.pushLeft();
	
	int xp, yp;
	map.getPlayerPos(xp, yp);
	
	QCOMPARE(map.getCase(1 ,2)->hasCaisse(), true);
	QCOMPARE(xp, 2);
	QCOMPARE(yp, 2);
}
//---------------------------------------------------------------------------------
void CTestMap::getSizeTest(void) {
	QString level=	" # #\n";
	level+=			"#   #\n";
	level+=			"  *\n";
	level+=			"#.  #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	int w, h;
	map.getSize(w, h);
	QCOMPARE(w, 5);
	QCOMPARE(h, 5);
}
//---------------------------------------------------------------------------------
void CTestMap::hasWinTest(void) {
	QString level=	" # #\n";
	level+=			"#$  #\n";
	level+=			"  *  \n";
	level+=			"#.  #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	QCOMPARE(map.hasWin(), false);
	
	level=	" # #\n";
	level+=	"#   #\n";
	level+=	"  *  \n";
	level+=	"#.  #\n";
	level+=	" # #";
	
	CMapReader::createMap(&map, level);
	QCOMPARE(map.hasWin(), true);
	
	level=	" # #\n";
	level+=	"#   #\n";
	level+=	"  *  \n";
	level+=	"#*  #\n";
	level+=	" # #";
	
	CMapReader::createMap(&map, level);
	QCOMPARE(map.hasWin(), true);
}
//---------------------------------------------------------------------------------
void CTestMap::cloneTest(void) {
	QString level=	" # #\n";
	level+=			"#$  #\n";
	level+=			"  *  \n";
	level+=			"#.  #\n";
	level+=			" # #";
	
	CMap map;
	CMapReader::createMap(&map, level);
	
	CMap *mapC=map.clone();
	for(int i=0;i<map.getNbCase();i++) {
		CCase* c=map.getCase(i);
		int x=c->getX();
		int y=c->getY();
		QString key=QString::number(x)+":"+QString::number(y);
		CCase* cC=mapC->getCase(x, y);
		
		QVERIFY(cC != 0);
		QCOMPARE(x, cC->getX());
		QCOMPARE(y, cC->getY());
		QCOMPARE(c->getCaseType(), cC->getCaseType());
		QCOMPARE(c->hasCaisse(), cC->hasCaisse());
		QCOMPARE(c->hasEmplacement(), cC->hasEmplacement());
	}
}
//---------------------------------------------------------------------------------
void CTestMap::cleanupTestCase(void) {
}
//---------------------------------------------------------------------------------